package com.laoxu.student.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoxu.student.business.entity.Student;
import com.laoxu.student.business.mapper.StudentMapper;
import com.laoxu.student.business.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laoxu.student.business.vo.StudentVO;
import com.laoxu.student.common.util.ExcelImportUtil;
import com.laoxu.student.common.util.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学生 服务实现类
 * </p>
 *
 * @author Laoxu
 * @since 2020-04-04
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public void pageQuery(Page<StudentVO> pageParam) {
        List<StudentVO> records = this.baseMapper.selectStudentList(pageParam);
        pageParam.setRecords(records);
        pageParam.setTotal(records.size());
    }

    @Transactional
    @Override
    public List<String> batchImport(MultipartFile file) throws Exception {

        //错误消息列表
        List<String> errorMsg = new ArrayList<>();

        //解析excel文件
        List<ArrayList<String>> rows = ExcelUtil.analysis(file);

        if(rows.size()<0){
            errorMsg.add("请填写数据");
            return errorMsg;
        }else {
            for (int i = 1;i<rows.size();i++){
                List<String> row = rows.get(i);

                // 不足32个（以导入模板列数为准）的补全,防止有空的单元格导致报错！！！
                for (int j = row.size(); j < 32; j++) {
                    row.add("");
                }

                //获取行
                System.out.println(row.get(0));

                Student student = new Student();
                student.setSno(row.get(0));
                student.setName(row.get(1));

                baseMapper.insert(student);//添加
            }

        }


        return errorMsg;
    }
}
